// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <gtest/gtest.h>
#include <climits>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cantor/parser/parser.h>
#include <cantor/utility/json_reader.h>
#include <tests/config.h>
#include <tests/paser/node_check.h>

int main(int argc, char *argv[]) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

namespace cantor::parser {

    class DataJson {
    public:
        static DataJson &instance() {
            static DataJson instance;
            return instance;
        }

        const nlohmann::ordered_json &json() const { return json_; }

    private:
        DataJson() {
            std::string json_path = turbo::str_cat(testing::test_root, "/data/type_list.json");
            auto rs = read_file(json_path);
            KCHECK(rs.ok());
            json_ = std::move(rs).value_or_die();
        }

        nlohmann::ordered_json json_;
    };

    void test_case(const std::string & type, bool show_json = false) {
        auto &json = DataJson::instance().json();
        auto list = json[type].get<std::vector<nlohmann::ordered_json>>();
        for (auto &item : list) {
            auto sql = item["type"].get<std::string>();
            KLOG(INFO)<<NOPREFIX<<"testing sql: "<<sql;
            cantor::parser::SqlParser parser;
            parser.parse(sql);
            ASSERT_EQ(cantor::parser::SUCCESS, parser.error)<<parser.syntax_err_str;
            ASSERT_EQ(1, parser.result.size());

            auto *stmt = (TypeStmt *) parser.result[0];
            if (item.contains("result")) {
                auto &r = item["result"];
                if (!r.empty()) {
                    check_type_stmt(stmt, r);
                }
            }
            KLOG(INFO)<<NOPREFIX<<"testing done ..............";
            if (show_json) {
                std::cout<<stmt->to_json().dump(4)<<std::endl;
            }
        }
    }
    TEST(test_parser, int_type) {
        test_case("int_type", false);
    }

    TEST(test_parser, float_type) {
        test_case("float_type", false);
    }

    TEST(test_parser, bool_type) {
        test_case("bool_type", false);
    }

    TEST(test_parser, decimal_type) {
        test_case("decimal_type", false);
    }

    TEST(test_parser, string_type) {
        test_case("string_type", false);
    }

    TEST(test_parser, times_type) {
        test_case("times_type", false);
    }

    TEST(test_parser, misc_type) {
        test_case("misc_type", false);
    }

    TEST(test_parser, map_type) {
        test_case("map_type", false);
    }
    TEST(test_parser, set_type) {
        test_case("set_type", false);
    }

    TEST(test_parser, tuple_type) {
        test_case("tuple_type", false);
    }

    TEST(test_parser, struct_type) {
        test_case("struct_type", false);
    }

    TEST(test_parser, enum_type) {
        test_case("enum_type", false);
    }
    TEST(test_parser, union_type) {
        test_case("union_type", false);
    }

    TEST(test_parser, proto_type) {
        test_case("proto_type", false);
    }


}  // namespace cantor::parser
